import * as cws from '../lib/net/client-websocket';
import * as wsEvent from '../lib/net/event-websocket';
import * as serverConfig from '../lib/config/config'
import * as protobuf from '../lib/protobuf/protobuf-msg';
import * as uuid from '../util/get-uuid';
export class webSocketTest{

public msgInterval:number = 1000;

public client:cws.ClientWebSocket;

private setIntervalID:any;

public main(){
    this.init();
    this.client = new cws.ClientWebSocket();
    let host = "127.0.0.1";
    //let host = "192.168.31.128";
    let port = 4000;
    this.client.Connection(host,port);
    this.client.on(wsEvent.Event.open,()=>this.opens());
    this.client.on(wsEvent.Event.clientMessage,(msg)=>this.message(msg));
}

public close(){
    this.client.getClient().close();
    clearInterval(this.setIntervalID);
    
}
public opens(){
   // console.log("ok");

    this.setIntervalID = setInterval(this.onSend,this.msgInterval,this);

}

public index = 0;

public str = `
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我我
我我我我我我我我我
`;

public onSend($this){
    let classList = protobuf.getMsgProto();       
    let newClass = new classList.login_login_c2s();
    newClass.user_name = new Date().getTime().toString();
    //newClass.user_name = uuid.getUUID();
    newClass.user_pass =  $this.str;
    let base = new classList.msg_base_c2s();
    base.setMsg(newClass.toArrayBuffer() as any);
    base.msg_code = protoBufMsg.head.msg_login_login_c2s;
    base.msg_type = Math.max(1,Math.floor(Math.random()*5));
    ($this.client as cws.ClientWebSocket).send(base.toArrayBuffer());
    //console.log("send ",newClass.user_name );
    $this.index++;
}

public _messageFun:any[]=[];

/**
 *  添加处理消息的模块
 * 
 * @export
 */
public init(){
    //_messageFun[protoBufMsg.head.msg_login_login_c2s] = getClass('./activity/one-day-award');
    
}

public consoleStr ='';
public message(msgClass:protoBufMsg.msg_base_s2c){

    let classList = protobuf.getMsgProto();
    let msg = classList.msg_base_s2c.decode(msgClass as any); 
    let msgObject = classList.login_login_c2s.decode(msg.msg as any);
    let lastTime = new Date().getTime() - parseInt( msgObject.getUserName() ); 
    this.consoleStr = lastTime+","+msg.toArrayBuffer().byteLength+","+msgObject.user_pass;
    //console.log(lastTime,msg.toArrayBuffer().byteLength,msgObject.user_pass);
    //console.log(new Date().getTime() - lastTime);
    //console.log(this.consoleStr);
   // _messageFun[msgClass.msg_code](msgClass.msg);
}

public getClass(_path:string){
    let clas = require(_path);
    if(clas){
        return clas.main;
    }else{
        console.error("game-server.index.getClass path not",_path);
    }
    return null;
}


}



/**
 * 建立1000个客户端，每个客户端每秒进行2次数据通信
 */

function check(){
    let arr:webSocketTest[]=[];

    // for(var i=0;i<500;i++){
    //     let wst = new webSocketTest();
    //     wst.msgInterval = 500;
    //     wst.main();
    //     arr.push(wst);
    // }


    let excList = [close,create,create,create,create];

    setTimeout(function(arr) {
        setInterval((arr)=>{
            if(connectionNum>maxConction){
                return;
            }
            let random = Math.floor(Math.random()*5);
            excList[random](arr);
        },200,arr);
        setInterval((arr)=>{
            let i = Math.floor(Math.random()*arr.length);
            console.log(arr[i].index,arr[i].consoleStr);
        },1000,arr);
    }, 1000,arr);

}

export function main(){
    check();
}

main();

export let connectionNum = 0;
export let maxConction = 500;
export function close(arr:webSocketTest[]){
    if(arr.length>0){
        let index =  Math.max(0,Math.floor(Math.random()*arr.length));
        arr[index].close();
        arr.slice(index,1);
        connectionNum--;
    }
}

export function create(arr:webSocketTest[]){
        let wst = new webSocketTest();
        wst.msgInterval = 1000;//Math.floor(Math.random()*2000);
        wst.main();
        arr.push(wst);
        connectionNum++;
}